#define GETOFFSETOF(TYPE,MEMBER)  (unsigned int)(&(((TYPE*)0)->MEMBER))						//获取偏移量
#define GET_STRUCT_ENTRY(TYPE,PTR,MEMBER) (TYPE*)(((char*)PTR)-(GETOFFSETOF(TYPE,MEMBER)))	//获取对应结构体地址

typedef struct  A
{
	int i;
	char b;
	double c;
}A;

int main()
{
	 A a = {0};

	double* ptr_c = &a.c;
	char* ptr_b = &a.b;
	int* ptr_i = &a.i;

	//计算对应成员偏移量
	printf("offsetof i: %d\n", GETOFFSETOF(A, i));
	printf("offsetof b: %d\n", GETOFFSETOF(A, b));
	printf("offsetof c: %d\n", GETOFFSETOF(A, c));

	//获取结构体地址
	printf("get struct address by i: %p\n", GET_STRUCT_ENTRY(A, ptr_i,i));
	printf("get struct address by b: %p\n", GET_STRUCT_ENTRY(A, ptr_b,b));
	printf("get struct address by c: %p\n", GET_STRUCT_ENTRY(A, ptr_c,c));

	//通过结构体地址获取成员地址
	A* Pstrcut = GET_STRUCT_ENTRY(A, ptr_i, i);
	printf("strcut member i address is: %p\n", &(Pstrcut->i));
	printf("strcut member b address is: %p\n", &(Pstrcut->b));
	printf("strcut member c address is: %p\n", &(Pstrcut->c));
	return 0;
}
